/*
 * log.c
 *
 *  Created on: 2009-8-26
 *      Author: wyb
 */

#include "log.h"

FILE *fp = NULL;
time_t file_time = 0;
char pn[256];

void set_project_name(char* src) {
	strcpy(pn, src);
}

int wyb_log(char* log_info) {
	char str_temp[256];
	time_t current_time;
	if (time(&current_time) != -1) {
		time_t temp = current_time - current_time % (24 * 60 * 60);
		struct tm *cur = localtime(&current_time);
		char date_str[200];

		if (current_time != file_time) {

			sprintf(str_temp, "%s_log", pn);
			if (access(str_temp, F_OK) == -1) {
				if (mkdir(str_temp, 0777)) {
					printf("log,can't creat folder %s", str_temp);
				}
			}

			strftime(date_str, 200, "%Y-%m-%d", cur);
			sprintf(str_temp, "%s_log/%s_%s.log", pn, pn, date_str);

			if (fp != NULL)
				fclose(fp);
			if ((fp = fopen(str_temp, "a+")) == NULL) {
				printf("log,can't open %s", str_temp);
				return 0;
			} else {
				file_time = temp;
			}
		}
		strftime(date_str, 200, "%Y-%m-%d,%H:%M:%S", cur);
		fprintf(fp, "%s    %s    %s\n", date_str, pn, log_info);
		fflush(fp);
	} else {
		printf("log,time error!\n");
		return 0;
	}
	return 1;
}

void wyb_log_close() {
	fclose(fp);
}
